package weaver.formmode.customjavacode.modeexpand.srm.cg;

import weaver.common.DateUtil;
import weaver.conn.RecordSet;
import weaver.formmode.customjavacode.AbstractModeExpandJavaCodeNew;
import weaver.general.BaseBean;
import weaver.general.Util;
import weaver.hrm.User;
import weaver.formmode.customjavacode.modeexpand.srm.util.SrmUtil;
import weaver.soa.workflow.request.RequestInfo;

import java.util.HashMap;
import java.util.Map;


/**
 * @Description: 运输类需求拆分新建模板的新建保存
 * @Param:
 * @return:
 * @Author: Dusongsong
 * @Date: 2021/3/23
 */
public class WeaYsxqcfNewSaveAction extends AbstractModeExpandJavaCodeNew {

    public Map<String, String> doModeExpand(Map<String, Object> param) {
        Map<String, String> result = new HashMap<String, String>();
        try {
            User user = (User) param.get( "user" );
            int billid = -1;//数据id
            int modeid = -1;//模块id
            RequestInfo requestInfo = (RequestInfo) param.get( "RequestInfo" );
            if (requestInfo != null) {
                billid = Util.getIntValue( requestInfo.getRequestid() );
                modeid = Util.getIntValue( requestInfo.getWorkflowid() );
                if (billid > 0 && modeid > 0) {
                    //------请在下面编写业务逻辑代码------
                    RecordSet rs = new RecordSet();
                    RecordSet rs1 = new RecordSet();
                    String cglx = "3";
                    String rwbh = SrmUtil.generTaskCode( cglx );//任务编号
                    String cgxqTable = SrmUtil.getCgxqTableType( cglx );
                    String cgrwTable = SrmUtil.getCgrwTableType( cglx );
                    String xqcfTable = SrmUtil.getXqcfTableType( cglx );
                    rs.writeLog( "WeaYsxqcfNewSaveAction-----------------" + billid );
                    String conFields = "xqly,xqdjr,xqdw,sfjcxq,xqrq,xqlx,xmmc,xmbh,xmxz,ygje,\n" +
                            "ysnr,zcdbjcsj,jhdbjcsj,cgy,sfbq,fj,bz,xqdh,xqbh,jcdh,\n" +
                            "htdh,xqfl,cgfs,pbfs,pid,bmid,fbid,nd,xmxzid,xqdwid,\n" +
                            "xqdwmc,xmid,cgqx";
                    //插入主表
                    String sql = "select " + conFields + ",xqdid from "+xqcfTable+" where id=?";
                    rs.executeQuery( sql, billid );
                    if (rs.next()) {
                        String xqly = Util.null2String( rs.getString( "xqly" ) );//需求来源
                        String xqdjr = Util.null2String( rs.getString( "xqdjr" ) );//需求对接人
                        String xqdw = Util.null2String( rs.getString( "xqdw" ) );//需求单位
                        String sfjcxq = Util.null2String( rs.getString( "sfjcxq" ) );//是否集采需求
                        String xqrq = Util.null2String( rs.getString( "xqrq" ) );//需求日期
                        String xqlx = Util.null2String( rs.getString( "xqlx" ) );//需求类型
                        String xmmc = Util.null2String( rs.getString( "xmmc" ) );//项目名称
                        String xmbh = Util.null2String( rs.getString( "xmbh" ) );//项目编号
                        String ygje = Util.null2String( rs.getString( "ygje" ) );//预估金额
                        String ysnr = Util.null2String( rs.getString( "ysnr" ) );//运输内容
                        String zcdbjcsj = Util.null2String( rs.getString( "zcdbjcsj" ) );//最迟定标决策时间
                        String jhdbjcsj = Util.null2String( rs.getString( "jhdbjcsj" ) );//计划定标决策时间
                        String cgy = Util.null2String( rs.getString( "cgy" ) );//采购员
                        String sfbq = Util.null2String( rs.getString( "sfbq" ) );//是否标前
                        String fj = Util.null2String( rs.getString( "fj" ) );//附件
                        String bz = Util.null2String( rs.getString( "bz" ) );//备注
                        String xqdh = Util.null2String( rs.getString( "xqdh" ) );//需求单号
                        String jcdh = Util.null2String( rs.getString( "jcdh" ) );//决策单号
                        String htdh = Util.null2String( rs.getString( "htdh" ) );//合同单号
                        String xqfl = Util.null2String( rs.getString( "xqfl" ) );//需求分类
                        String cgfs = Util.null2String( rs.getString( "cgfs" ) );//采购方式
                        String pbfs = Util.null2String( rs.getString( "pbfs" ) );//评标方式
                        String xmxz = Util.null2String( rs.getString( "xmxz" ) );//项目性质
                        String xqbh = Util.null2String( rs.getString( "xqbh" ) );//需求包号
                        String pid = Util.null2String( rs.getString( "pid" ) );//PID
                        String bmid = Util.null2String( rs.getString( "bmid" ) );//部门ID
                        String fbid = Util.null2String( rs.getString( "fbid" ) );//分部ID
                        String xqdid = Util.null2String( rs.getString( "xqdid" ) );//需求单ID
                        String xqdwmc = Util.null2String( rs.getString( "xqdwmc" ) );//需求单位名称
                        String xmid = Util.null2String( rs.getString( "xmid" ) );//项目id
                        String cgqx = Util.null2String( rs.getString( "cgqx" ) );//采购权限
                        String nd = Util.null2String( rs.getString( "nd" ) );//年度
                        String xmxzid = Util.null2String( rs.getString( "xmxzid" ) );//项目性质ID
                        String xqdwid = Util.null2String( rs.getString( "xqdwid" ) );//需求单位ID
                        String rwzt = "0";//任务状态
                        String cfdid = Util.null2String(billid);
                        String formmodeid = SrmUtil.getFormmodeId(cglx);
                        String modedatacreatertype = "0";
                        String modedatacreater = user.getUID() + "";
                        String modedatacreatedate = DateUtil.getCurrentDate();
                        String fullDate = DateUtil.getFullDate();
                        String modedatacreatetime = fullDate.substring( 11, 19 );
                        String sql1 = "insert into "+cgrwTable+" (" +conFields+",rwzt,rwbh,xqdid,cfdid,cglx,"+
                                "formmodeid,modedatacreater,modedatacreatertype,modedatacreatedate,modedatacreatetime) " +
                                " values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?," +
                                        "?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?," +
                                        "?,?,?)";
                        Object[] params = {xqly,xqdjr,xqdw,sfjcxq,xqrq,xqlx,xmmc,xmbh,xmxz,ygje,
                                ysnr,zcdbjcsj,jhdbjcsj,cgy,sfbq,fj,bz,xqdh,xqbh,jcdh,
                                htdh,xqfl,cgfs,pbfs,pid,bmid,fbid,nd,xmxzid,xqdwid,
                                xqdwmc,xmid,cgqx,rwzt,rwbh,xqdid,cfdid,cglx,
                                formmodeid, modedatacreater, modedatacreatertype, modedatacreatedate, modedatacreatetime};
                        if (rs.executeUpdate( sql1, params )) {
                            //插入明细表
                            String mainid = SrmUtil.getRwbh2Id( rwbh, cglx );
                            SrmUtil.getNewModeRight(cgy,formmodeid,mainid);
                            String comFieldDt1 = "hwmc,ggxh,dw,sl,zcl,ztj,qydd,dddd,qyrq,dhrq,ysfs,cgsl,sysl";
                            String sql2 = "select "+comFieldDt1+",xqmxid from "+xqcfTable+"_dt1 where mainid=? and gxcf=1";
                            rs1.executeQuery( sql2, billid );
                            while (rs1.next()) {
                                String hwmc = Util.null2String( rs1.getString( "hwmc" ) );//货物名称
                                String ggxh = Util.null2String( rs1.getString( "ggxh" ) );//规格型号
                                String dw = Util.null2String( rs1.getString( "dw" ) );//单位
                                String sl = Util.null2String( rs1.getString( "sl" ) );//数量
                                String zcl = Util.null2String( rs1.getString( "zcl" ) );//总重量
                                String ztj = Util.null2String( rs1.getString( "ztj" ) );//总体积
                                String qydd = Util.null2String( rs1.getString( "qydd" ) );//起运地点
                                String dddd = Util.null2String( rs1.getString( "dddd" ) );//到达地点
                                String qyrq = Util.null2String( rs1.getString( "qyrq" ) );//起运日期
                                String dhrq = Util.null2String( rs1.getString( "dhrq" ) );//到货日期
                                String ysfs = Util.null2String( rs1.getString( "ysfs" ) );//运输方式
                                String cgsl = Util.null2String( rs1.getString( "cgsl" ) );//采购数量
                                String sysl = Util.null2String( rs1.getString( "sysl" ) );//剩余数量
                                String xqmxid = Util.null2String( rs1.getString( "xqmxid" ) );//需求明细ID
                                String sql3 = "insert into "+cgrwTable+"_dt1(mainid,"+comFieldDt1+",xqmxid) " +
                                        "values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
                                Object[] params1 = {mainid, hwmc,ggxh,dw,sl,zcl,ztj,qydd,dddd,qyrq,dhrq,ysfs,cgsl,sysl,xqmxid};
                                rs1.executeUpdate( sql3, params1 );
                                //更新物料采购需求池的勾选拆分
                                rs1.executeUpdate("update "+cgxqTable+"_dt1 set cfbj=1 where id=?",xqmxid);
                            }
                            if(SrmUtil.getDtlCounts(xqdid,cglx)){
                                rs1.executeUpdate("update "+cgxqTable+" set fplx=1 where id in ("+xqdid+")");
                            }
                        }
                    }
                }
            }
        } catch (Exception e) {
            result.put( "errmsg", "自定义出错信息" );
            result.put( "flag", "false" );
        }
        return result;
    }
}